package com.jshare.interceptor;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.jshare.action.ArticleAction;
import com.jshare.action.MemberAction;
import com.jshare.action.TieziAction;
import com.jshare.entity.Member;
import com.jshare.util.*;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

//继承可以指定方法的拦截器接口

public class LoginInter extends MethodFilterInterceptor {

  private static final long serialVersionUID = -72827036782663208L;
 
  private static  final Logger log = Logger.getLogger(LoginInter.class);
 
     public final static String SESSION_KEY="member";//保存 user 在Session中
     public final static String ADMIN_KEY = "admin";
     public final static String GOTO_URL_KEY="GOING_TO";  
      
     
     protected String doIntercept(ActionInvocation invocation) throws Exception   
      {    
          HttpServletRequest request=ServletActionContext.getRequest();   
            
          HttpSession session=request.getSession();   
          //通过session 判断用户是否登录
          if(session!=null && session.getAttribute(SESSION_KEY)!=null) {
        	  	  Member member = (Member) session.getAttribute("member");
    	           Object o = invocation.getAction();
    	            if (o instanceof MemberAction) {
    	            	MemberAction action = (MemberAction) o;
    	               action.setMember(member);
    	            }else if(o instanceof ArticleAction)
    	            {
    	            	ArticleAction action = (ArticleAction) o;
     	               action.setM(member);
    	            }else if(o instanceof TieziAction)
    	            {
    	            	TieziAction action = (TieziAction)o;
    	            	action.setMember(member);
    	            }
                  return invocation.invoke();   
          }
          //这里是关键点了 设置客户原来请求的url地址   
          setToGoingURL(request, session, invocation);
          return Action.LOGIN;   
      }   
     private void setToGoingURL(HttpServletRequest request,HttpSession session,ActionInvocation invocation)   
      {   
          //如果referer不为空 直接使用它。如果为空我们分别获得命名空间，action名,以及请求参数   
          //从新构造成一个URL保存在session中   
          String url=request.getHeader("referer");   
          log.info("待转向URL:"+request.getHeader("referer"));   
          if(url==null || url.equals(""))   
              {   
                  url="";   
                  String path=request.getContextPath();   
                  String actionName=invocation.getProxy().getActionName();   
                  String nameSpace=invocation.getProxy().getNamespace();   
                  if(StringUtil.validateNull(nameSpace))   
                      {   
                          url = url+path+nameSpace;   
                             
                      }   
                  if(StringUtil.validateNull(actionName))   
                      {   
                          url = url+"/"+actionName+".action"+"?";   
                      }   
                     
                  Map<String,String[]> zzMap = request.getParameterMap();   
                  if(zzMap!=null)   
                      {   
                          for(String s:zzMap.keySet())   
                              {   
                                  String[] value=zzMap.get(s);   
                                  for(String val:value)   
                                      {   
                                          url=url+s+"="+val+"&";   
                                      }   
                                     
                              }   
                      }   
                  log.info("完整URL:"+url);   
              }   
                 session.setAttribute(GOTO_URL_KEY, url);   
      }

}